<template>
  <div class="app-container">
    <el-form class="formList"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px">
      <el-row>
        <el-form-item label="客户姓名"
          prop="customerName">
          <el-input v-model="queryParams.customerName"
            placeholder="请输入客户姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="推荐门店"
          prop="fromStoreId">
          <el-select v-model="queryParams.toStoreId"
            placeholder="请选择投放门店"
            clearable
            size="small">
            <el-option v-for="dict in storeInFoOptions"
              :key="dict.storeId"
              :label="dict.storeName"
              :value="dict.storeUid" />
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="选择日期" prop="sendDate">
          <el-date-picker
            v-model="queryParams.sendDate"
            type="daterange"
            align="right"
            value-format="yyyy-MM-dd"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item> -->

        <el-form-item label="状态"
          prop="acceptStatus">
          <el-select v-model="queryParams.acceptStatus"
            placeholder="请选择状态"
            clearable
            size="small">
            <el-option v-for="dict in acceptStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left: 22px">
          <el-button type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh"
            size="mini"
            @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd">推荐客户</el-button>
      </el-col>
      <!-- <el-col :span="1.5" style="margin-left: 10px">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:brand:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:brand:remove']"
          >删除</el-button
        >
      </el-col> -->

      <right-toolbar :showSearch.sync="showSearch"
        @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="divTable">
      <el-table border
        v-loading="loading"
        :data="brandList"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection"
          width="55"
          align="center" />

        <el-table-column label="客户姓名"
          align="center"
          min-width="100"
          prop="customerName" />
        <el-table-column label="客户性别"
          align="center"
          prop="customerSexStr" />

        <el-table-column label="手机号码"
          align="center"
          min-width="120"
          prop="phone" />
        <el-table-column label="客户简介"
          align="center"
          prop="customerBrief" />

        <el-table-column label="沟通凭证"
          align="center"
          min-width="100"
          prop="chatProof">
          <template slot-scope="scope">
            <el-image v-if="scope.row.chatProof != null && scope.row.chatProof != ''"
              slot="reference"
              :src="scope.row.chatProof"
              class="image-rows"
              :preview-src-list="[scope.row.chatProof]"></el-image>
          </template>
        </el-table-column>

        <el-table-column label="接收门店"
          align="center"
          min-width="120"
          prop="toStoreIdStr" />

        <el-table-column label="推荐时间"
          align="center"
          prop="createTime"
          width="180"
          sortable>
          <template slot-scope="scope">
            <span>{{
    parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
}}</span>
          </template>
        </el-table-column>
        <el-table-column label="接收状态"
          align="center"
          prop="acceptStatusStr" />
        <el-table-column label="接受信息"
          align="center"
          min-width="120"
          prop="acceptMsg" />

        <el-table-column label="审核时间"
          align="center"
          prop="acceptTime"
          width="180"
          sortable>
          <template slot-scope="scope">
            <span>{{
    parseTime(scope.row.acceptTime, "{y}-{m}-{d} {h}:{i}:{s}")
}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作"
          align="center"
          fixed="right"
          width="100"
          class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-if="scope.row.acceptStatus == '-1'"
              type="text"
              icon="el-icon-plus"
              size="mini"
              @click="handleStatusChange(scope.row)"
              v-hasPermi="['device:brand:add']">重新推荐</el-button>
            <el-button v-if="scope.row.acceptStatus == '1'"
              type="text"
              icon="el-icon-zoom-in"
              size="mini"
              @click="handleDetail(scope.row)">详情</el-button>
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['device:brand:edit']"
              >接受</el-button
            >
            <el-button
              v-if="scope.row.dataStatus == '0'"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['device:brand:remove']"
              >拒绝</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <!-- 添加或修改客户/系列对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="750px"
      append-to-body
      @keyup.enter.native="submitForm">

      <el-form :model="form"
        ref="form"
        :rules="rules"
        label-width="80px">
        <el-form-item label="客户姓名"
          prop="customerName"
          class="item_flex_1">
          <el-input disabled
            v-model.trim="form.customerName"
            style="width: 350px" />
          <el-button type="text"
            class="ml10"
            @click="toSelectData">选择客户</el-button>
          <!-- <el-button v-if="List!=null"
            class="ml10"
            type="text"
            @click="toDeleteData"> <span class="delte_icon_text">删除</span> </el-button> -->
        </el-form-item>
        <el-form-item label="客户性别"
          class="item_flex_1">
          <el-input disabled
            v-model.trim="form.customerSexStr"
            style="width: 350px" />
        </el-form-item>
        <el-form-item label="联系电话"
          class="item_flex_1">
          <el-input disabled
            v-model.trim="form.phone"
            style="width: 350px" />
        </el-form-item>

        <el-form-item label="选择门店"
          prop="toStoreId"
          class="item_flex_1">
          <el-select v-model="form.toStoreId"
            placeholder="请选择门店"
            clearable
            size="small"
            style="width: 350px">
            <el-option v-for="dict in storeInFoOptionsA"
              :key="dict.storeId"
              :label="dict.storeName"
              :value="dict.storeUid" />
          </el-select>
        </el-form-item>

        <el-form-item label="客户简介"
          prop="customerBrief">
          <el-input v-model="form.customerBrief"
            type="textarea"
            resize="none"
            :rows="4"
            placeholder="请输入客户简介"
            class="select-normal"
            show-word-limit
            maxlength="120"
            style="width: 350px" />
        </el-form-item>

        <el-form-item label="沟通凭证">
          <imageArraySize :imageStr.sync="form.chatProof"
            :key="form.chatProof"
            :limit="1"
            ref="childsss"></imageArraySize>
        </el-form-item>

      </el-form>

      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择客户 -->
    <commodityRecommendRadio :flag="showCard"
      ref="customerRcmd"
      @multipleSelection="multipleSelection"
      @toCloseBox="toCloseBox" />

    <el-dialog :title="title"
      :visible.sync="openA"
      width="750px"
      append-to-body
      @keyup.enter.native="submitForm"
      z-index="index">
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="140px">
        <el-table border
          v-loading="loading"
          :data="brandListA"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection"
            width="55"
            align="center" />

          <el-table-column label="修改时间"
            align="center"
            prop="createTime"
            width="180"
            sortable>
            <template slot-scope="scope">
              <span>{{
    parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
}}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="totalA > 0"
          :total="totalA"
          :page.sync="queryParamsA.pageNum"
          :limit.sync="queryParamsA.pageSize"
          @pagination="handleUpdate(null)" />
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="详情"
      :visible.sync="openXf"
      width="650px"
      append-to-body>

      <el-form :model="form"
        ref="form"
        :rules="rules"
        label-width="80px">
        <el-form-item label="客户姓名"
          class="item_flex_1">
        {{ form.customerName }}
        </el-form-item>
       
        <el-form-item label="客户性别"
          class="item_flex_1">
        {{ form.customerSexStr }}
        </el-form-item>

        <el-form-item label="手机号码"
          class="item_flex_1">
        {{ form.phone }}
        </el-form-item>

        <el-form-item label="沟通凭证"
          class="item_flex_1">
   


        <el-image v-if="form.chatProof" :z-index="zIndex" :preview-src-list="[form.chatProof]"
              :src="form.chatProof" style="
                      width: 80px;
                      height: 80px;
                      margin-left: 10px;
                      margin-top: 10px;
                      border: 1px solid #ebebeb;
                      border-radius: 6px;
                    ">
            </el-image>

        </el-form-item>

        <el-form-item label="接收门店"
          class="item_flex_1">
        {{ form.toStoreIdStr }}
        </el-form-item>

        <el-form-item label="推荐时间"
          class="item_flex_1">
        {{   parseTime(form.createTime, "{y}-{m}-{d} {h}:{i}:{s}") }}
        </el-form-item>

        <el-form-item label="接收状态"
          class="item_flex_1">
        {{ form.acceptStatusStr }}
        </el-form-item>

        <el-form-item label="接受信息"
          class="item_flex_1">
        {{ form.acceptMsg }}
        </el-form-item>

      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import {
  listScore,
  listDetail,
  delScore,
  addScore,
  updateScore,
  updateScoreStatus,
} from "@/api/service/recommend";
import commodityRecommendRadio from '@/components/Activity/commodityRecommendRadio.vue'
import { listItem } from "@/api/service/score";
import {
  listStoreSelect,
  listRecommend
} from "@/api/platform/store"
import {

  listConsumptionByUid
} from "@/api/customer/Info"
import imageArraySize from "@/components/ImageUpload/imageArraySize";
export default {
  name: "Score",
  components: {
    imageArraySize,
    commodityRecommendRadio,
  },
  data () {
    return {
      query: {
        pageNum: 1,
        pageSize: 10,
        time: "",
        customerUid: null,
      },
      openXf: false,
      list: [],
      timeTotal: 0,
      index: 1500,
      technicainIdA: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //选中数组的名字
      names: [],
      List: [],
      // 非单个禁用
      single: true,
      storeInFoOptions: [],
      storeInFoOptionsA: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 总条数
      totalA: 0,
      // 客户/系列表格数据
      brandList: [],
      brandListA: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openA: false,
      // 用于类别关联的分类显示
      dialogVisible: false,
      //常用状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        toStoreId: null,
        acceptStatus: null,
        itemName: null,
        dataStatus: null,
      },
      acceptStatusOptions: [],
      sendDate: null,
      sTime: null,
      eTime: null,
      // 查询参数
      queryParamsA: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
        dataStatus: null,
      },
      type: "add",
      postList: [],

      // 表单参数
      form: {
        customerName: '',
        toStoreId: '',
        dialogImageUrl: "",
        dataPriority: "",
      },
      // 表单校验
      rules: {
        toStoreId: [
          {
            required: true,
            message: "推荐门店不能为空",
            trigger: "blur",
          },
        ],

        customerName: [
          {
            required: true,
            message: "客户不能为空",
            trigger: "blur",
          },
        ],
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      showCard: false,
      selectCustomerName: ''
    };
  },
  created () {
    this.getList();

    this.getDicts("sys_data_status").then((response) => {
      this.statusOptions = response.data;
    });

    this.getDicts("accept_status").then((response) => {
      this.acceptStatusOptions = response.data;
    });

    listStoreSelect().then(response => {
      this.storeInFoOptions = response.data || [];
    });
    listRecommend().then((response) => {
      this.storeInFoOptionsA = response.data || [];
    });


    this.toGetPlanRoleList();
  },
  methods: {
    getSecondList () {
      listConsumptionByUid(this.query.customerUid, this.query).then(response => {
        this.list = response.rows;
        this.timeTotal = response.total;
        this.openXf = true;
      })
    },
    // 选择客户
    toSelectData () {
      this.showCard = true
      this.$nextTick(() => {
        if (this.form.customerName) {
          this.$refs['customerRcmd'].greet([this.form])
        }
      })
    },
    handleDetail (row) {
      // this.query.customerUid = row.customerId
      // this.getSecondList();
      this.form = row;
      this.openXf = true;
    },
    toDeleteData () {

    },
    multipleSelection (row) {
      console.log(row)
      this.form = row
      this.showCard = false
    },
    // 关闭
    toCloseBox () {
      this.showCard = false
    },
    dataPriorityInput (value) {
      let js = value + "";
      return js.replace(/^(0+)|[^\d]+/g, "");
    },
    /** 查询客户/系列列表 */
    getList () {
      this.loading = true;
      if (
        this.queryParams.sendDate != null &&
        this.queryParams.sendDate.length > 0
      ) {
        let arr = this.queryParams.sendDate;
        this.queryParams.sTime = arr[0] + " 00:00:00";
        this.queryParams.eTime = arr[1] + " 23:59:59";
      }
      listScore(this.queryParams).then((response) => {
        this.brandList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    async toGetPlanRoleList (value) {
      let res = await listItem({ itemType: value });
      if (res) {
        this.postList = res.rows;
      }
    },

    handleRemove (file, fileList) { },
    handlePictureCardPreviewLogo (file) {
      this.logoUr = file.url;
      this.dialogVisible = true;
    },
    handlePictureCardPreviewImg (file) {
      this.deviceImage = file.url;
      this.dialogVisible = true;
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.openA = false;
      this.reset();
    },
    // 关联的取消按钮
    cancelScore () {
      this.openScore = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        customerName: '',
        itemName: null,
        logoUrl: null,
        itemType: null,
        deviceImage: null,
        dataPriority: 10,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 客户菜单 */
    getMenuTreeselect () {
      menuTreeselect().then((response) => {
        this.menuOptions = response.data;
      });
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.itemId);
      this.names = selection.map((item) => item.itemName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      //用于禁用删除
      if (this.multiple == false) {
        for (var i = 0; i < selection.length; i++) {
          var element = selection[i];
          if ("1" === element.dataStatus) {
            this.multiple = true;
            break;
          }
        }
      }
    },
    // 状态修改
    handleStatusChange (row) {

      this.$confirm(
        '确定重新推荐客户名称为"' + row.customerName + '"的客户吗？',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          row.acceptStatus = "0"
          return updateScoreStatus(row.recommendId, row.acceptStatus);
        })
        .then(() => {
          this.msgSuccess("操作成功");
          this.getList();
        })
        ;
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset();

      this.open = true;
      this.title = "意向客户推荐";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      if (row != null) {
        this.form.acceptStatus = "0";
        this.queryParamsA.pageNum = 1;
        this.queryParamsA.pageSize = 10;
      }

      listDetail(this.queryParamsA).then((response) => {
        this.brandListA = response.rows;
        this.totalA = response.total;
        this.openA = true;
        this.title = "重新推荐客户";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.customerId = this.form.customerUid;
          this.form.customerSex = this.form.sex;
          addScore(this.form).then((response) => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const itemIds = row.itemId || this.ids;
      const names = row.itemName || this.names;
      this.$confirm('是否确认删除客户名称为"' + names + '"的客户?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delScore(itemIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },

    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "device/brand/export",
        {
          ...this.queryParams,
        },
        `商品客户.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.treeOne {
  display: inline-block;
  margin-right: 200px;
}

.treeTwo {
  display: inline-block;
}

.image-rows {
  width: 80px;
  height: auto;
}
</style>
